home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Screen savers / Basic Black 1.3.1 Folder / Basic Black / Source / BBcdev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  11.9 KB  |  406 lines  |  [TEXT/R*ch]

  1. /**********************************************************
  2.  *    Basic Black © 1993 by Mason L. Bliss
  3.  *    - version 1.3b5 - 11/6/1993 -
  4.  *
  5.  *    This cdev code uses the sample cdev class included with
  6.  *    the Think C 5 compiler.
  7.  **********************************************************/
  8.  
  9. /**********************************************************
  10.  *    Version History: (cdev code)
  11.  *
  12.  *    1.2        11/93
  13.  *    Added in patch global for menu bar kluge. We now use the
  14.  *    Gestalt 'BBlk' selector to determine whether or not the
  15.  *    patch has actually been installed.
  16.  *
  17.  *    1.1        7/93
  18.  *    Switched over to the object-oriented cdev model included
  19.  *    with the Think C 5 compiler. Also, fixed a bug that
  20.  *    caused crashes under System 6.
  21.  *
  22.  *    1.0        6/93
  23.  *    Converted cdev code from SC.010.EditTextCdev, and added
  24.  *    code to modify Basic Black's CNFG resource.
  25.  **********************************************************/
  26.  
  27. #include <cdev.h>
  28. #include <GestaltEqu.h>
  29. #include "BBcdev.h"
  30. #include "BB.h"
  31.  
  32.  
  33.  
  34. /**********************************************************
  35.  *    BBcdev::Init
  36.  *
  37.  *    This method initializes our 'once' counter.
  38.  **********************************************************/
  39. void BBcdev::Init(void)
  40. {
  41.     once = false;
  42.     inherited::Init();
  43. }
  44.  
  45.  
  46.  
  47. /**********************************************************
  48.  *    BBcdev::Idle
  49.  *
  50.  *    This is a bloody kludge we have to do because it seems
  51.  *    that we don't get activate messages under System 6.
  52.  **********************************************************/
  53. void BBcdev::Idle(void)
  54. {
  55.     if (!once)
  56.         Activate();
  57. }
  58.  
  59.  
  60.  
  61. /**********************************************************
  62.  *    BBcdev::Activate
  63.  *
  64.  *    This method reads the values stored in the CNFG
  65.  *    resource, and sets the dialog control values
  66.  *    accordingly.
  67.  **********************************************************/
  68. void BBcdev::Activate(void)
  69. {
  70.     Handle            configHandle;
  71.     ControlHandle    theControl;
  72.     short            controlValue;
  73.     long            theLong, width, height;
  74.     Rect            box;
  75.     Str255            theString;
  76.  
  77.     once = true;    // grumble, grumble
  78.     
  79.     configHandle = GetIndResource('PREF', 1);        // get the first 'PREF'
  80.     if (ResError()) {
  81.         Error(cdevResErr);
  82.         return;
  83.     }
  84.  
  85.     /* Load Picture Resources */
  86.     pictHandles[0] = GetResource('PICT', topLeftPict);
  87.     pictHandles[1] = GetResource('PICT', topRightPict);
  88.     pictHandles[2] = GetResource('PICT', botRightPict);
  89.     pictHandles[3] = GetResource('PICT', botLeftPict);
  90.  
  91.     /* get the sleep corner info */
  92.     sleepNowRect = **configHandle;
  93.     GetDItem(dp, lastItem - initDev + sleepNow,
  94.                 &controlValue, &theControl, &box);
  95.     SetDItem(dp, lastItem - initDev + sleepNow,
  96.                 controlValue, pictHandles[sleepNowRect], &box);
  97.     
  98.     width  = ((box.right - box.left) / 2);
  99.     height = ((box.bottom - box.top) / 2);
  100.     
  101.     /* Set up some small rects */
  102.     smallSNRect[0] = box;
  103.     smallSNRect[0].right -= width;
  104.     smallSNRect[0].bottom -= height;
  105.     smallSNRect[1] = box;
  106.     smallSNRect[1].left += width;
  107.     smallSNRect[1].bottom -= height;
  108.     smallSNRect[2] = box;
  109.     smallSNRect[2].left += width;
  110.     smallSNRect[2].top += height;
  111.     smallSNRect[3] = box;
  112.     smallSNRect[3].right -= width;
  113.     smallSNRect[3].top += height;
  114.     
  115.     /* get the wake corner info */
  116.     neverSleepRect = *(*configHandle + 1);
  117.     GetDItem(dp, lastItem - initDev + neverSleep,
  118.                 &controlValue, &theControl, &box);
  119.     SetDItem(dp, lastItem - initDev + neverSleep,
  120.                 controlValue, pictHandles[neverSleepRect], &box);
  121.  
  122.     width  = ((box.right - box.left) / 2);
  123.     height = ((box.bottom - box.top) / 2);
  124.  
  125.     /* Set up more small rects */
  126.     smallNSRect[0] = box;
  127.     smallNSRect[0].right -= width;
  128.     smallNSRect[0].bottom -= height;
  129.     smallNSRect[1] = box;
  130.     smallNSRect[1].left += width;
  131.     smallNSRect[1].bottom -= height;
  132.     smallNSRect[2] = box;
  133.     smallNSRect[2].left += width;
  134.     smallNSRect[2].top += height;
  135.     smallNSRect[3] = box;
  136.     smallNSRect[3].right -= width;
  137.     smallNSRect[3].top += height;
  138.             
  139.     /* get the clock checkbox */
  140.     GetDItem(dp, lastItem - initDev + clockOn, &controlValue, &theControl, &box);
  141.     if ((short) *(*configHandle + 3))
  142.         SetCtlValue(theControl, 1);
  143.     
  144.     /* get the "screen saver on" checkbox */
  145.     GetDItem(dp, lastItem - initDev + screenOn, &controlValue, &theControl, &box);
  146.     if ((short) *(*configHandle + 4))
  147.         SetCtlValue(theControl, 1);
  148.     
  149.     /* get the "Show Startup Icon" checkbox */
  150.     GetDItem(dp, lastItem - initDev + startupIcon, &controlValue, &theControl, &box);
  151.     if ((short) *(*configHandle + 6))
  152.         SetCtlValue(theControl, 1);
  153.     
  154.     /* get the "Fade To White" checkbox */
  155.     GetDItem(dp, lastItem - initDev + fadeWhite, &controlValue, &theControl, &box);
  156.     if ((short) *(*configHandle + 7))
  157.         SetCtlValue(theControl, 1);
  158.  
  159.     /* get the "Menubar Kluge" checkbox */
  160.     GetDItem(dp, lastItem - initDev + menuKluge, &controlValue, &theControl, &box);
  161.     if ((short) *(*configHandle + 8))
  162.         SetCtlValue(theControl, 1);
  163.     
  164.     /* get the idle time edit text item */
  165.     GetDItem(dp, lastItem - initDev + idleMins, &controlValue, &theControl, &box);
  166.     theLong = (long) ((short) *(*configHandle + 2));
  167.     NumToString(theLong, theString);
  168.     SetIText(theControl, theString);
  169.     SelIText(dp, lastItem - initDev + idleMins, 0, 32767);
  170.     
  171.     /* get the refresh time edit text item */
  172.     GetDItem(dp, lastItem - initDev + refreshSecs, &controlValue, &theControl, &box);
  173.     theLong = (long) ((short) *(*configHandle + 5));
  174.     NumToString(theLong, theString);
  175.     SetIText(theControl, theString);
  176.     
  177.     ReleaseResource(configHandle);
  178. }
  179.  
  180.  
  181.  
  182. /**********************************************************
  183.  *    BBcdev::Deactivate
  184.  *
  185.  *    This method releases the 'PICT' resources.
  186.  **********************************************************/
  187. void BBcdev::Deactivate(void)
  188. {
  189.     short    i;
  190.     
  191.     for (i = 0; i < 4; ++i)
  192.         ReleaseResource(pictHandles[i]);
  193.     
  194.     inherited::Deactivate();
  195. }
  196.  
  197.  
  198.  
  199. /**********************************************************
  200.  *    BBcdev::ItemHit
  201.  *
  202.  *    This handles dialog item hits.
  203.  **********************************************************/
  204. void BBcdev::ItemHit(short theItem)
  205. {
  206.     ControlHandle    theControl, tempControl;
  207.     short            controlValue, count;
  208.     Rect            box;
  209.  
  210.     GetDItem(dp, lastItem - initDev + theItem, &controlValue, &theControl, &box);
  211.     switch (theItem) {
  212.  
  213.         case sleepNow:
  214.             GlobalToLocal(&event->where);
  215.             if (PtInRect(event->where, &smallSNRect[topLeftRect])
  216.                                             && neverSleepRect != topLeftRect)
  217.                 sleepNowRect = topLeftRect;
  218.             else if (PtInRect(event->where, &smallSNRect[topRightRect])
  219.                                             && neverSleepRect != topRightRect)
  220.                 sleepNowRect = topRightRect;
  221.             else if (PtInRect(event->where, &smallSNRect[botRightRect])
  222.                                             && neverSleepRect != botRightRect)
  223.                 sleepNowRect = botRightRect;
  224.             else if (PtInRect(event->where, &smallSNRect[botLeftRect])
  225.                                             && neverSleepRect != botLeftRect)
  226.                 sleepNowRect = botLeftRect;
  227.             else {
  228.                 SysBeep(5);
  229.                 break;
  230.             }
  231.             
  232.             SetDItem(dp, lastItem - initDev + sleepNow,
  233.                     controlValue, pictHandles[sleepNowRect], &box);
  234.             InsetRect(&box, 3, 3);
  235.             EraseRect(&box);
  236.             InsetRect(&box, -3, -3);
  237.             DrawPicture(pictHandles[sleepNowRect], &box);
  238.             break;
  239.  
  240.         case neverSleep:
  241.             GlobalToLocal(&event->where);
  242.             if (PtInRect(event->where, &smallNSRect[topLeftRect])
  243.                                             && sleepNowRect != topLeftRect)
  244.                 neverSleepRect = topLeftRect;
  245.             else if (PtInRect(event->where, &smallNSRect[topRightRect])
  246.                                             && sleepNowRect != topRightRect)
  247.                 neverSleepRect = topRightRect;
  248.             else if (PtInRect(event->where, &smallNSRect[botRightRect])
  249.                                             && sleepNowRect != botRightRect)
  250.                 neverSleepRect = botRightRect;
  251.             else if (PtInRect(event->where, &smallNSRect[botLeftRect])
  252.                                             && sleepNowRect != botLeftRect)
  253.                 neverSleepRect = botLeftRect;
  254.             else {
  255.                 SysBeep(5);
  256.                 break;
  257.             }
  258.             
  259.             SetDItem(dp, lastItem - initDev + neverSleep,
  260.                     controlValue, pictHandles[neverSleepRect], &box);
  261.             InsetRect(&box, 3, 3);
  262.             EraseRect(&box);
  263.             InsetRect(&box, -3, -3);
  264.             DrawPicture(pictHandles[neverSleepRect], &box);
  265.             break;
  266.  
  267.         case screenOn:
  268.         case clockOn:
  269.         case startupIcon:
  270.         case fadeWhite:
  271.         case menuKluge:
  272.             controlValue = GetCtlValue(theControl);
  273.             controlValue = !controlValue;
  274.             SetCtlValue(theControl, controlValue);
  275.             break;
  276.  
  277.         case remButton:
  278.             RememberValues();
  279.             break;
  280.  
  281.         default:
  282.             break;
  283.     }
  284. }
  285.  
  286.  
  287.  
  288. /**********************************************************
  289.  *    BBcdev::Key
  290.  *
  291.  *    This filters out invalid key presses.
  292.  **********************************************************/
  293. void BBcdev::Key(short theKey)
  294. {
  295.     ControlHandle    theControl;
  296.     short            controlValue;
  297.     Rect            box;
  298.     long            time;
  299.  
  300.     if (((theKey < '0') || (theKey > '9')) && (theKey != 8) && (theKey != 13) && (theKey != 9) && ((theKey < 28) || (theKey > 31))) {
  301.         event->what = nullEvent;
  302.         SysBeep(5);
  303.     }
  304.     if (theKey == 13) {
  305.         event->what = nullEvent;
  306.         GetDItem(dp, lastItem - initDev + remButton, &controlValue, &theControl, &box);
  307.         HiliteControl(theControl, 1);
  308.         for (time = TickCount() + 15; TickCount() < time;)
  309.             SystemTask();
  310.         HiliteControl(theControl, 0);
  311.         RememberValues();
  312.     }
  313. }
  314.  
  315.  
  316.  
  317. /**********************************************************
  318.  *    BBcdev::RememberValues
  319.  *
  320.  *    This saves the information in the CNFG resource, and,
  321.  *    if the Basic Black INIT is loaded, sets the values of
  322.  *    its patch globals in memory.
  323.  **********************************************************/
  324. void BBcdev::RememberValues(void)
  325. {
  326.     Handle            configHandle;
  327.     ControlHandle    theControl;
  328.     short            controlValue, count;
  329.     long            theLong;
  330.     Rect            box;
  331.     Str255            theString;
  332.     PatchGlobalsPtr    pgPtr;
  333.  
  334.     configHandle = GetIndResource('PREF', 1);        // get the first 'PREF'
  335.  
  336.     /* set the idle time information */
  337.     GetDItem(dp, lastItem - initDev + idleMins, &controlValue, &theControl, &box);
  338.     GetIText(theControl, theString);
  339.     StringToNum(theString, &theLong);
  340.     *(*configHandle + 2) = (short) theLong;
  341.     if (theLong < 1 || theLong > 127) {
  342.         Alert(128, NULL);
  343.         SelIText(dp, lastItem - initDev + idleMins, 0, 32767);
  344.         return;
  345.     }
  346.  
  347.     /* set the refresh time information */
  348.     GetDItem(dp, lastItem - initDev + refreshSecs, &controlValue, &theControl, &box);
  349.     GetIText(theControl, theString);
  350.     StringToNum(theString, &theLong);
  351.     *(*configHandle + 5) = (short) theLong;
  352.     if (theLong < 1 || theLong > 127) {
  353.         Alert(128, NULL);
  354.         SelIText(dp, lastItem - initDev + refreshSecs, 0, 32767);
  355.         return;
  356.     }
  357.     
  358.     /* set the sleep corner information */
  359.     **configHandle = sleepNowRect;
  360.     
  361.     /* set the wake corner information */
  362.     *(*configHandle + 1) = neverSleepRect;
  363.  
  364.     /* set the clock information */
  365.     GetDItem(dp, lastItem - initDev + clockOn, &controlValue, &theControl, &box);
  366.     *(*configHandle + 3) = GetCtlValue(theControl);
  367.  
  368.     /* set the "screen saver on" information */
  369.     GetDItem(dp, lastItem - initDev + screenOn, &controlValue, &theControl, &box);
  370.     *(*configHandle + 4) = GetCtlValue(theControl);
  371.  
  372.     /* set the "Startup Icon" information */
  373.     GetDItem(dp, lastItem - initDev + startupIcon, &controlValue, &theControl, &box);
  374.     *(*configHandle + 6) = GetCtlValue(theControl);
  375.  
  376.     /* set the "Fade To White" information */
  377.     GetDItem(dp, lastItem - initDev + fadeWhite, &controlValue, &theControl, &box);
  378.     *(*configHandle + 7) = GetCtlValue(theControl);
  379.  
  380.     /* set the "Zero Menu Bar" information */
  381.     GetDItem(dp, lastItem - initDev + menuKluge, &controlValue, &theControl, &box);
  382.     *(*configHandle + 8) = GetCtlValue(theControl);
  383.  
  384.     /* If the extension's installed, then update it's patch globals */
  385.     if (Gestalt('BBlk', (long *) &pgPtr) == noErr) {
  386.         pgPtr->pgMustSleep = false;
  387.         pgPtr->pgSleepRect = (short) **configHandle;
  388.         pgPtr->pgWakeRect = (short) *(*configHandle + 1);
  389.         pgPtr->pgIdleTicks = (long) ((short) *(*configHandle + 2)) * 3600;
  390.         pgPtr->pgBouncingClock = (Boolean) *(*configHandle + 3);
  391.         pgPtr->pgMustSave = (Boolean) *(*configHandle + 4);
  392.         pgPtr->pgRefreshTime = ((short) *(*configHandle + 5)) * 60;
  393.         if (((short) *(*configHandle + 7)) == 0) {        // Fade to black
  394.             StuffHex(&(pgPtr->pgForePat), "\p0000000000000000");
  395.             StuffHex(&(pgPtr->pgBackPat), "\pFFFFFFFFFFFFFFFF");
  396.         } else {                                        // Fade to white
  397.             StuffHex(&(pgPtr->pgForePat), "\pFFFFFFFFFFFFFFFF");
  398.             StuffHex(&(pgPtr->pgBackPat), "\p0000000000000000");
  399.         }
  400.         pgPtr->pgMenubarKluge = (Boolean) *(*configHandle + 8);
  401.     }
  402.  
  403.     ChangedResource(configHandle);
  404.     WriteResource(configHandle);
  405.     ReleaseResource(configHandle);
  406. }